//
//  MCSendHongBaoCollectionViewCell.m
//  XDYL
//
//  Created by MC on 2018/3/20.
//  Copyright © 2018年 TLYL01. All rights reserved.
//

#import "MCSendHongBaoCollectionViewCell.h"
#import "MCSendHongBaoTableViewCell.h"
#import <MJRefresh/MJRefresh.h>
#import "MCSendHongBaoHeaderView.h"
#import "MCSendRecordListAndTotalModel.h"
#import "MCHongBaoRecordProperty.h"

@interface MCSendHongBaoCollectionViewCell()
<
UITableViewDelegate,
UITableViewDataSource
>

typedef void(^Compeletion)(BOOL result, NSDictionary *data );

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)MCSendHongBaoHeaderView *headerView;
@property(nonatomic, strong)MCSendRecordListAndTotalModel * sendRecordListAndTotalModel;


@property(nonatomic, strong) NSString * statTime2;
@property(nonatomic, strong) NSString * endTime2;
@property(nonatomic, strong) NSString * CurrentPageIndex2;
@property(nonatomic, strong) NSString * CurrentPageSize2;
@property(nonatomic, assign) BOOL IsHistory2;
@property(nonatomic, strong) NSString * GetHongBaoType;
@property(nonatomic, strong)ExceptionView * exceptionView;

@property (nonatomic,strong)NSMutableArray <MCRecordsModel *> * dataMarray;


@end

@implementation MCSendHongBaoCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        [self initView];
    }
    return self;
}


- (void)initView{
    
    self.backgroundColor=RGB(231,231,231);
    
    [self setProperty];
    
    [self createUI];
    
    [self refreashData];
    
}

- (void)refreashSendHongBaoProperty{
    
    MCSendHBRecordProperty * SendProperty = [MCSendHBRecordProperty sharedMCSendHBRecordProperty];
    _statTime2=SendProperty.statTime;
    _endTime2=SendProperty.endTime;
    _CurrentPageIndex2=SendProperty.CurrentPageIndex;
    _CurrentPageSize2=SendProperty.CurrentPageSize;
    _IsHistory2=SendProperty.IsHistory;
    _GetHongBaoType=SendProperty.GetHongBaoType;
    
}
#pragma mark==================setProperty======================
-(void)setProperty{
    
    self.backgroundColor=RGB(231, 231, 231);
    _dataMarray=[[NSMutableArray alloc]init];
    [self refreashSendHongBaoProperty];
}
-(MCSendHongBaoHeaderView *)headerView{
    if (!_headerView) {
        _headerView=[[MCSendHongBaoHeaderView alloc]init];
        //        _headerView.delegate=self;
        _headerView.frame=CGRectMake(0, 0, G_SCREENWIDTH, [MCSendHongBaoHeaderView computeHeight:nil]);
    }
    return _headerView;
}
#pragma mark==================createUI======================
-(void)createUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.layer.cornerRadius=5;
    _tableView.clipsToBounds=YES;
    _tableView.tableHeaderView=self.headerView;
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreashData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(sendHongloadMoreData)];
    
    [self addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(15);
        make.left.equalTo(self.mas_left).offset(10);
        make.right.equalTo(self.mas_right).offset(-10);
        make.bottom.equalTo(self.mas_bottom).offset(5);
    }];
    
}

#pragma mark-下拉刷新
- (void)refreashData{
    
    [self.dataMarray removeAllObjects];
    [self.exceptionView dismiss];
    self.exceptionView = nil;
    self.tableView.mj_footer.hidden=NO;
    self.tableView.mj_header.hidden=NO;
    [BKIndicationView showInView:self];
    __weak __typeof__ (self) wself = self;
    _CurrentPageIndex2=@"1";
    
    [self loadData:^(BOOL result, NSDictionary *data) {
        [wself.tableView.mj_footer endRefreshing];
        [wself.tableView.mj_header endRefreshing];
        if (result) {
            
            [wself setData:data];
            
        }else{
            wself.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeRequestFailed];
            wself.exceptionView.heightH=G_SCREENHEIGHT-kStatusBarAndNavigationBarHeight-40;
            wself.exceptionView.actionColor=RGB(255,69,69);
            ExceptionViewAction *action = [ExceptionViewAction actionWithType:ExceptionCodeTypeRequestFailed handler:^(ExceptionViewAction *action) {
                [wself.exceptionView dismiss];
                wself.exceptionView = nil;
                [wself refreashData];
            }];
            [wself.exceptionView addAction:action];
            [wself.exceptionView showInView:self.tableView];
        }
    }];
   
}

-(void)setData:(NSDictionary *)dic{
    
    MCSendRecordsModel * model = [MCSendRecordsModel getModelWithDic:dic];
    
    if ( (model.SendRecords.Records.count%15!=0) || (model.SendRecords.Records.count==0) ) {
        [self.tableView.mj_footer endRefreshing];
        self.tableView.mj_footer.hidden=YES;
    }
    [self.dataMarray addObjectsFromArray:model.SendRecords.Records];
    
    if (self.dataMarray.count<1) {
        self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoData];
        self.exceptionView.heightH=G_SCREENHEIGHT-64-40;
        [self.exceptionView showInView:self.tableView];
        return;
    }
    
    [self.tableView reloadData];
    
    _headerView.dataSource=model.SendRecordTotal;

}

-(void)sendHongloadMoreData{

    _CurrentPageIndex2=[NSString stringWithFormat:@"%d",[_CurrentPageIndex2 intValue]+1];
    [BKIndicationView showInView:self];
    __weak __typeof__ (self) wself = self;
    [self loadData:^(BOOL result, NSDictionary *data) {
        [wself.tableView.mj_footer endRefreshing];
        [wself.tableView.mj_header endRefreshing];
        if (result) {
            [wself setData:data];
        }else{
            [SVProgressHUD showErrorWithStatus:@"加载失败"];
            wself.CurrentPageIndex2=[NSString stringWithFormat:@"%d",[_CurrentPageIndex2 intValue]-1];
        }
    }];

}

#pragma mark==================loadData======================
-(void)loadData:(Compeletion)compeletion{
    
    NSDictionary * dic=@{
                         @"BeginTime":self.statTime2,//开始时间
                         @"EndTime":self.endTime2,//结束时间
                         @"CurrentPageIndex":self.CurrentPageIndex2,//
                         @"CurrentPageSize":self.CurrentPageSize2,
                         @"IsHistory":[NSNumber numberWithBool:self.IsHistory2],
                         @"State":self.GetHongBaoType
                         };
    
    MCSendRecordListAndTotalModel * sendRecordListAndTotalModel = [[MCSendRecordListAndTotalModel alloc]initWithDic:dic];
    [sendRecordListAndTotalModel refreashDataAndShow];
    self.sendRecordListAndTotalModel = sendRecordListAndTotalModel;
    
    sendRecordListAndTotalModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSString *errorCode) {
        
        compeletion(NO,nil);
    };
    
    sendRecordListAndTotalModel.callBackSuccessBlock = ^(id manager) {
        
        compeletion(YES,manager);
        
    };
    
}

#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataMarray.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return [MCReceivedHongBaoHeaderView computeHeight:nil];
//}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    return nil;
//}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [MCSendHongBaoTableViewCell computeHeight:nil];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *reuseIdentifier =[NSString stringWithFormat:@"MCSendHongBaoTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    MCSendHongBaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[MCSendHongBaoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (self.dataMarray.count>0) {
        cell.dataSource=self.dataMarray[indexPath.row];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_dataMarray.count>0) {
        MCRecordsModel * model = [_dataMarray objectAtIndex:indexPath.row];
        NSString * Sysid =model.SysId;
        if (self.goToDetailSendHongBaoBlock) {
            self.goToDetailSendHongBaoBlock(Sysid);
        }
    }
    
    
}




/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end



